//
//  NetworkEngine.m
//  Project
//
//  Created by Jason on 15/12/2.
//  Copyright © 2015年 Happy. All rights reserved.
//

#import "NetworkEngine.h"
#import <AFNetworkActivityIndicatorManager.h>

@implementation NetworkEngine
+(NetworkEngine *)engine{
    
    static NetworkEngine *engine = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    
    engine = [[NetworkEngine alloc] init];
});
    
    return engine;
}

+(AFHTTPRequestOperationManager *)manager{
    
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 45;
    manager.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
                                                                              @"text/html",
                                                                              @"text/json",
                                                                              @"text/javascript"]];
    
    manager.operationQueue.maxConcurrentOperationCount = 3;
    return manager;

    
}

-(AFHTTPRequestOperation *)doPOSTFetchRequest:(NSString *)urlStr withParmeter:(NSDictionary *)parmeter withRemarkDic:(NSDictionary*)remarkDic withSucBlock:(RequestSucBlock)sucblock withSucWithMsgBlock:(RequestSucWithMsgBlock)sucMsgBlock withFailBlock:(RequestFailWithBlock)failBlock{
    
    AFHTTPRequestOperationManager *manager = [NetworkEngine  manager];
    
   /** 如果需求特别的时候可以用下面的方法自己处理NSURLRequest
    NSURLRequest*request = [manager requestSerializer] requestWithMethod:<#(nonnull NSString *)#> URLString:<#(nonnull NSString *)#> parameters:<#(nullable id)#> error:<#(NSError *__autoreleasing  _Nullable * _Nullable)#>
     
    manager HTTPRequestOperationWithRequest:<#(nonnull NSURLRequest *)#> success:<#^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject)success#> failure:<#^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error)failure#>
    
    */
    
    NSString *fullUrl = [NSString stringWithFormat:@"%@%@",Host,urlStr];
    AFHTTPRequestOperation *op = [manager POST:fullUrl parameters:parmeter.mutableCopy success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       
         //处理responseObject 1.数据 2.提示性数据
        
        
        NSLog(@"the data is %@",responseObject);
        
        if(/* DISABLES CODE */ (1)){
            
            if (sucblock) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                   
                    sucblock (responseObject);
                    
                });
                
            }
            
        }else{
            
                    NSLog(@"the data is %@",responseObject);
            
            if (sucMsgBlock) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                   
                    sucMsgBlock(responseObject,@"msg");
                    
                });
                
            }
            
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        
        if (failBlock) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
               
                failBlock(error ,@"failMsg");
                
            });
        }
        
    }];

    return op;
}

-(AFHTTPRequestOperation *)doUploadFileRequest:(NSString *)urlStr withParmeter:(NSDictionary *)parmeter withRemarkDic:(NSDictionary*)remarkDic withSucBlock:(RequestSucBlock)sucblock withSucWithMsgBlock:(RequestSucWithMsgBlock)sucMsgBlock withFailBlock:(RequestFailWithBlock)failBlock{
    
    AFHTTPRequestOperationManager *manager = [NetworkEngine  manager];
    
    /** 如果需求特别的时候可以用下面的方法自己处理NSURLRequest
     
     manager HTTPRequestOperationWithRequest:<#(nonnull NSURLRequest *)#> success:<#^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject)success#> failure:<#^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error)failure#>
     **/
    
    
    NSString *fullUrl = [NSString stringWithFormat:@"%@%@",Host,urlStr];
    
    AFHTTPRequestOperation *op = [manager POST:fullUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //data 数据在调用方法的时候处理，通过parameter传递过来，这里暂时不做处理。请根据实际情况自己处理
        
        
         [formData appendPartWithFileData:[NSData dataWithContentsOfFile:@"filePath"] name:@"file" fileName:@"filename" mimeType:@"image/jpeg"];
        
    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        if (sucMsgBlock) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                sucMsgBlock(responseObject,@"msg");
                
            });
            
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        if (failBlock) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                failBlock(error ,@"failMsg");
                
            });
        }
    }];

    return op;
}


@end
